package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理相册相关请求的控制器
 *
 * @author Tan
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/albums")
@Api( tags = "01. 相册处理模块")
@Validated
public class AlbumController {
    @Autowired
    private IAlbumService albumService;

    public AlbumController() {
        log.debug("创建控制器对象:AlbumController");
    }

    //  http://localhost:8080/albums/add-new?name=相册001&description=相册001的简介&sort=199
    @PostMapping( "/add-new")
    @ApiOperation(value="添加相册")
    @ApiOperationSupport(order = 101)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDTO albumAddNewDTO){
        log.debug("开始处理[添加相册]的请求,参数{}",albumAddNewDTO);
//        try{
//            albumService.addNew(albumAddNewDTO);
//            log.debug("数据插入成功");
//            return "添加相册成功";
//        }catch (ServiceException e){
//            log.debug(e.getMessage());
//            return e.getMessage();
//        }
        //除了try catch处理异常以外还可以用统一异常处理机制,该机制是由SpringMVC提供的
        albumService.addNew(albumAddNewDTO);
        log.debug("数据插入成功");
        return JsonResult.ok();
    }

//    @PostMapping("/test")
//    @ApiOperation(value = "[已过期]测试异常抛出")
//    @ApiOperationSupport(order = 901)
//    public String test(){
//        throw new ServiceException("这是一个测试抛出的异常");
//    }

    /**
     * RESTful风格,'{}'为占位符,获取参数要在参数前加上@PathVariable注解
     * @param id
     * @return
     */
    //      http://localhost:8080/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation(value = "根据id删除相册 ")
    @ApiOperationSupport(order = 201)
    @ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除相册失败,尝试删除的id无效") @PathVariable Long id){
        String message = "尝试删除id为["+id+"]的相册";
        log.debug(message);
        albumService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id修改相册信息")
    @ApiOperationSupport(order = 320)
    public JsonResult<Void> updateById(AlbumUpdateDTO albumUpdateDTO){
        log.debug("开始处理[修改相册信息]请求,参数:{}",albumUpdateDTO);
        albumService.updateById(albumUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<AlbumListItemVO>> list(){
        log.debug("开始处理[查询相册列表]请求,无参数");
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/{id}/getStandById")
    @ApiOperation(value = "查询相册详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<AlbumStandardVO> getStandById(@PathVariable Long id) {
        log.debug("开始处理[查询相册详情]的请求,参数:{}", id);
        AlbumStandardVO vo = albumService.getStandardById(id);
        return JsonResult.ok(vo);
    }
}